import asyncio
from typing import Union
import math
from PyCmpltrtok.common import sep

PREFIX = 'delay: '


async def delay(delay_seconds: int, name: Union[str, None] = None, interval: float = 1.0) -> int:
    sep(f'Enter delay({delay_seconds}, {name}, {interval})')
    print(PREFIX + f'{name}: sleeping for {delay_seconds} second(s) by {name}')
    num = int(math.ceil(delay_seconds / interval))
    for i in range(num):
        n = i + 1
        print(PREFIX + f'{name}: sleeping for {delay_seconds} second(s) by {name} for {n}th interval')
        await asyncio.sleep(interval)
    print(PREFIX + f'{name}: finished sleeping for {delay_seconds} second(s) by {name}')
    sep(f'Leave delay({delay_seconds}, {name}, {interval})')
    return delay_seconds
